package com.maven.front.service.manageLogin;

import com.maven.front.bean.*;
import com.maven.front.bean.SysFunction;
import com.maven.front.bean.SysUser;
import com.maven.front.dto.SysRoleDto;
import com.maven.front.mapper.SysFunctionMapper;
import com.maven.front.mapper.SysRoleFunctionMapper;
import com.maven.front.mapper.SysRoleMapper;
import com.maven.front.mapper.SysUserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionService {


    @Resource
    private SysFunctionMapper sysFunctionMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRoleFunctionMapper sysRoleFunctionMapper;

    @Resource
    private SysUserMapper sysUserMapper;
    //查询登录权限信息
    public Result selectById(String token){
        SysUser sysUser=(SysUser)redisTemplate.opsForValue().get(token);
        if(StringUtils.isEmpty(token)){
            return new Result(101,"token为空");
        }else{
            List<SysFunction> sysFunction=this.sysFunctionMapper.selectByPrimaryKey(sysUser.getRoleId());
            if(ObjectUtils.isEmpty(sysFunction)){
                return new Result(201,"查询失败");
            }else{
                return new Result(200,"查询成功",sysFunction);
            }
        }
    }

    //查询全部权限信息
    public List<SysFunction> selectId(){

        return this.sysFunctionMapper.select();
    }

    //查询所有角色信息和权限信息
    public Result selectRole(SysRole sysRole){
        List<SysRoleDto> sysRoleDto=new ArrayList<SysRoleDto>();
//        查询所有角色信息的名称和id
        List<SysRole> list=this.sysRoleMapper.select(sysRole);
//        循环，根据角色id获取需要的数据
        for(int i=0;i<list.size();i++){
            SysRoleDto sysRoleDto1 = new SysRoleDto();
//            角色id
            int role_id=list.get(i).getRoleId();
//           根据角色id
            List<SysFunction> li1=this.sysFunctionMapper.selectList(role_id);//父权限
            List<SysFunction> li2=this.sysFunctionMapper.selectList1(role_id);//子权限
            sysRoleDto1.setRoleId(role_id);
            sysRoleDto1.setRoleName(list.get(i).getRoleName());
            sysRoleDto1.setMap1(li1);
            sysRoleDto1.setMap2(li2);
            sysRoleDto.add(sysRoleDto1);
        }
        if(CollectionUtils.isEmpty(sysRoleDto)){
            return new  Result(2001,"无数据");
        }else{
            return new Result(200,"success",sysRoleDto);
        }


    }
    //父权限集合
    public List<SysFunction> sel1(){
        List<SysFunction> listf=this.sysFunctionMapper.sel1();
        return listf;
    }
    //子权限集合
    public List<SysFunction> sel2(){
        List<SysFunction> listz=this.sysFunctionMapper.sel2();
        return listz;
    }

    public Result selectAll(){
        List<SysFunction> listf = sel1();
        List<SysFunction> listz= sel2();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("listf",listf);
        map.put("listz",listz);
        if(CollectionUtils.isEmpty(map)){
            return new Result(201,"无数据");
        }else{
            return new Result(200,"success",map);
        }
    }

    //添加角色信息
    public Result  insertRole(SysRole sysRole){
        List<SysRole> sysRole1=this.sysRoleMapper.select(null);
        for(SysRole s:sysRole1){
            if(s.getRoleName().equals(sysRole.getRoleName())){
                return new Result(205,"角色名重复，请重新添加", null);
            }
        }
        int res=this.sysRoleMapper.insert(sysRole);
        if(res>0){
            int id = sysRole.getRoleId();
            return new Result(200,"success",sysRole);
        }else{
            return new Result(201,"添加失败");
        }
    }

    //根据roleName查询roleId
//       public Result selRoleId(SysRole sysRole){
//           SysRole sysRole1=this.sysRoleMapper.selectRoleId(sysRole);
//           if(ObjectUtils.isEmpty(sysRole1)){
//               return new Result(201,"没有查到");
//           }else{
//               return new Result(200,"success",sysRole1.getRoleId());
//           }
//       }

    //新增权限
    public Result insertRoleFunction(List<SysRoleFunction> list){
        int res=this.sysRoleFunctionMapper.insertRole(list);
        if(res>0){
            return new Result(200,"新增权限成功",true);
        }else{
            return new Result(201,"新增权限失败");
        }

    }

    //查询单个ID的权限
    public Result  selectgetById(Integer roleId){
        List<SysRoleFunction> list=this.sysRoleFunctionMapper.selectgetById(roleId);
        if(CollectionUtils.isEmpty(list)){
            return new Result(201,"无数据");
        }else{
            return new Result(200,"success",list);
        }

    }

    public Result deleteRole(Integer roleId){
        int res=this.sysRoleFunctionMapper.deleteRole(roleId);
        if(res>0){
            return new Result(200,"删除成功",true);
        }else{
            return new Result(201,"删除失败");
        }
    }

    //添加父权限
    public int insertFunction(SysFunction sysFunction){
        return this.sysFunctionMapper.insertFunction(sysFunction);
    }

    //添加子权限
    public int insertFunctionz(SysFunction sysFunction){
        return this.sysFunctionMapper.insertFunction(sysFunction);
    }
}
